package cn.hnucm.userservice.controller;

import cn.hnucm.userservice.entity.*;
import cn.hnucm.userservice.mapper.TbUserBasisMapper;
import cn.hnucm.userservice.service.TbLogService;
import cn.hnucm.userservice.service.TbUserBasisService;
import cn.hnucm.userservice.utils.IPUtils;
import cn.hnucm.userservice.utils.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 用户基础信息表 前端控制器
 * </p>
 *
 * @author zjl
 * @since 2021-12-08
 */
@Api(tags = "tb_User_Basis类")
@RestController
@RequestMapping("/userservice/tbUserBasis")
public class TbUserBasisController {

    @Autowired
    private TbUserBasisService tbUserBasisService;
    @Autowired
    private TbLogService tbLogService;
    @Autowired
    private HttpServletRequest req;

    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    /**
     * 路径： /userservice/tbUserBasis/getUserBasis/1
     * 描述: 根据userId查询tb_user_basis表
     *
     * @param userId 用户id
     * @return List<TbUserBasis>
     */

    @GetMapping("/getUserBasis/{userId}")
    public List<TbUserBasis> getUserBasisById(@PathVariable("userId") int userId) {
        long startTime = System.currentTimeMillis();
        List<TbUserBasis> tbUserBasisList = tbUserBasisService.findTbUserBasisById(userId);
        long endTime = System.currentTimeMillis();
        Date d = new Date(startTime);
        int userid;
        if (tbUserBasisList.size() == 0) {
            userid = 0;
        } else {
            userid = userId;
        }
        TbLog tbLog = new TbLog(Integer.parseInt(req.getHeader("userId")), "查找用户基本信息", "getUserBasisById", "{userId:" + userId + "}", endTime - startTime, IPUtils.getIpAddr(req), dateFormat.format(d));
        tbLogService.addTbLog(tbLog);
        return tbUserBasisList;
    }

//    @PostMapping("/getUserBasisMessage/{userAccount}")
//    public List<TbUserBasicMessage> getUserBasisMessage(@PathVariable("userAccount") String userAccount) {
//        return TbUserBasicMessageService.findTbUserBasicMessageByAccount();
//    }

    /**
     * 路径： /userservice/tbUserBasis/addUserBasis
     * 描述: 向tb_user_basis表添加用户，该方法不建议调用，可能导致数据异常
     *
     * @param tbUserBasis 用户信息
     * @return boolean
     */

    @PostMapping("/addUserBasis")
    public boolean addUserBasis(@RequestBody TbUserBasis tbUserBasis) {
        long startTime = System.currentTimeMillis();
        boolean tBasis = tbUserBasisService.addTbUserBasis(tbUserBasis);
        long endTime = System.currentTimeMillis();
        Date d = new Date(startTime);
        int userid;
        if (tBasis == true) {
            userid = tbUserBasis.getUserId();
        } else {
            userid = 0;
        }
        TbLog tbLog = new TbLog(Integer.parseInt(req.getHeader("userId")), "新增用户基本信息", "addUserBasis", "{tbUserBasis:" + tbUserBasis + "}", endTime - startTime, IPUtils.getIpAddr(req), dateFormat.format(d));
        tbLogService.addTbLog(tbLog);
        return tBasis;
    }

    /**
     * 路径： /userservice/tbUserBasis/updateUserBasis
     * 描述: 根据用户ID修改tb_user_basis表的用户信息
     *
     * @param tbUserBasis 用户信息
     * @return boolean
     */

    @PostMapping("/updateUserBasis")
    public boolean updateUserBasis(@RequestBody TbUserBasis tbUserBasis) {
        long startTime = System.currentTimeMillis();
        boolean tBasis = tbUserBasisService.updateTbUserBasis(tbUserBasis);
        long endTime = System.currentTimeMillis();
        Date d = new Date(startTime);
        int userid;
        if (tBasis == true) {
            userid = tbUserBasis.getUserId();
        } else {
            userid = 0;
        }
        TbLog tbLog = new TbLog(Integer.parseInt(req.getHeader("userId")), "更新用户基本信息", "updateUserBasis", "{tbUserBasis:" + tbUserBasis + "}", endTime - startTime, IPUtils.getIpAddr(req), dateFormat.format(d));
        //System.out.println(tbLog);
        tbLogService.addTbLog(tbLog);
        return tBasis;
    }

    /**
     * 路径： /userservice/tbUserBasis/deleteUserBasis
     * 描述: 根据用户ID删除tb_user_basis表的用户信息
     *
     * @param userId 用户id
     * @return boolean
     */

    @PostMapping("/deleteUserBasis")
    public boolean deleteUserBasis(@RequestBody int userId) {
        long startTime = System.currentTimeMillis();
        boolean tBasis = tbUserBasisService.deleteTbUserBasis(userId);
        long endTime = System.currentTimeMillis();
        Date d = new Date(startTime);
        int userid;
        if (tBasis == true) {
            userid = userId;
        } else {
            userid = 0;
        }
        TbLog tbLog = new TbLog(Integer.parseInt(req.getHeader("userId")), "删除用户基本信息", "deleteUserBasis", "{userId:" + userId + "}", endTime - startTime, IPUtils.getIpAddr(req), dateFormat.format(d));
        //System.out.println(tbLog);
        tbLogService.addTbLog(tbLog);
        return tBasis;
    }

    @GetMapping("/getPayMessage/{userId}")
    public List<PayMessage> getPayMessage(@PathVariable("userId") int userId) {
        return tbUserBasisService.getPayMessage(userId);
    }

    @GetMapping("/updateAssertById/{userId}/{userAsset}")
    public Boolean updateAssertById(@PathVariable("userId") int userId, @PathVariable("userAsset") Float userAsset) {
        return tbUserBasisService.updateAssertById(userId, userAsset);
    }
}

